package com.liu.resume.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.liu.resume.MyApplication;
import com.liu.resume.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/20.
 */
public abstract class BaseActivity extends BackgroundActivity {
    protected Context context;
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutView());
        ((MyApplication) getApplication()).addActivity(this);
        context = this;
        activity = this;
        ButterKnife.bind(this);
        //实例化Toast
        ToastUtil.setContext(this);
        initView();
    }
    /**
     * 获取布局控件
     */
    protected abstract int getLayoutView();

    /**
     * 初始化View的一些数据
     */
    protected abstract void initView();

    /*
      * 前往哪个类
      * */
    protected void goActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }
    /**
     * 给界面添加透明度
     */
    public static void backgroundAlpha(Activity activity, float bgAlpha) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus)
            EventBus.getDefault().unregister(this);
    }


    /**
     * 隐藏软键盘
     */
    public void hideSoftInput() {
        /** 隐藏软键盘 **/
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    private boolean isRegisterEventBus = false;//判断是否进行EventBus的注册，用于取消注册时的判断

    /**
     * 注册EventBus ，如果调用此方法，必须要声明接收函数，否则报错
     * onEvent(AnyTypeEvent event)/onEventMainThread(AnyTypeEvent event)..
     * 具体参见EventBus使用介绍http://blog.csdn.net/harvic880925/article/details/40787203
     */
    protected void registerEventBus() {
        EventBus.getDefault().register(this);
        isRegisterEventBus = true;
    }
}
